package controller

import (
	"ip_server/logger"
	"ip_server/service/model"
	"ip_server/service/param"
	"ip_server/util"
	"strconv"

	"github.com/gin-gonic/gin"
)

type BlackController struct {
}

// List 组列表接口
func (grp *BlackController) List(c *gin.Context) {
	logid := util.GenerateLogid() // 生成日志ID，用于追踪和记录该请求的日志
	var page,pagesize int
	if c.Query("page") != "" {
		page, err := strconv.Atoi(c.Query("page"))
		if err != nil {
			page = 1
		} else if page < 1 {
			page = 1
		} else {
			page = page
		}
	}
	if c.Query("page_size") != "" {
		pagesize, err := strconv.Atoi(c.Query("page_size"))
		if err != nil {
			pagesize = 1
		} else if page < 1 {
			pagesize = 1
		} else {
			pagesize = pagesize
		}
	}
	var pmodel model.Black
	total,glist, err := pmodel.List(param.BlacksReqBefore{
		Page:page,
		PageSize:pagesize,
	})
	if err != nil {
		// 查询失败时，记录错误日志，并设置错误信息到上下文中
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	c.Set("res", gin.H{
		"status": 0,
		"msg":    "success",
		"data":   map[string]interface{}{
			"list":glist,
			"page":page,
			"page_size":pagesize,
			"total":total,
		},
	})
}

// Add 组添加接口
func (grp *BlackController) Add(c *gin.Context) {
	logid := util.GenerateLogid() // 生成日志ID，用于追踪和记录该请求的日志
	//尝试从请求中解析用户请求参数
	var req model.Black
	err := c.ShouldBindJSON(&req)
	if err != nil {
		// 如果解析参数失败，记录错误日志，并设置错误信息到上下文中
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	if req.Fingerprint == "" && req.IP == "" {
		logger.Error(logid, "param is empty")
		c.Set("err", "group name is empty")
		return
	}
	var gmodel model.Black
	if req.Fingerprint != "" {
		ginfo, _ := gmodel.GetInfoByName("fingerprint",req.Fingerprint)
		if ginfo.ID != 0 {
			logger.Error(logid, "data is exist")
			c.Set("err", "data is exist")
			return
		}
	}
	if req.IP != "" {
		ginfo, _ := gmodel.GetInfoByName("ip",req.IP)
		if ginfo.ID != 0 {
			logger.Error(logid, "data is exist")
			c.Set("err", "data is exist")
			return
		}
	}
	err = gmodel.Add(&req)
	if err != nil {
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	c.Set("res", gin.H{
		"status": 0,
		"msg":    "success",
		"data":   "",
	})
}

// Update 组编辑接口
func (grp *BlackController) Update(c *gin.Context) {
	logid := util.GenerateLogid() // 生成日志ID，用于追踪和记录该请求的日志

	var req model.Black
	err := c.ShouldBindJSON(&req)
	if err != nil {
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	//if req.GroupName == ""  || req.GroupID == ""{
	//	logger.Error(logid, "group name is empty")
	//	c.Set("err", "参数不能为空")
	//	return
	//}
	var pmodel model.Black
	err = pmodel.Update(&req)
	if err != nil {
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	c.Set("res", gin.H{
		"status": 0,
		"msg":    "success",
		"data":   "",
	})
}

// Delete 组删除接口
func (grp *BlackController) Delete(c *gin.Context) {
	logid := util.GenerateLogid() // 生成日志ID，用于追踪和记录该请求的日志
	id := c.Query("id")
	if id == "" {
		logger.Error(logid, "参数不能为空")
		c.Set("err", "参数不能为空")
		return
	}
	var pmodel model.Black
	err := pmodel.Delete(id)
	if err != nil {
		logger.Error(logid, err.Error())
		c.Set("err", err.Error())
		return
	}
	c.Set("res", gin.H{
		"status": 0,
		"msg":    "success",
		"data":   "",
	})
}
